package com.yangguang.huahui.ui.activitys;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yangguang.huahui.R;
import com.yangguang.huahui.ui.adapters.TypePageAdapter;
import com.yangguang.huahui.ui.fragments.BaseFragment;
import com.yangguang.huahui.ui.fragments.PublishListFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/3/29.
 */

public class PublishListActivity extends BaseActivity{
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.mViewPager)
    ViewPager mViewPager;
    @BindView(R.id.mTabLayout)
    TabLayout mTabLayout;
    List<String> datas = new ArrayList<>();

    private List<BaseFragment> mFragments;
    private List<String> mTitles = new ArrayList<>();
    private TypePageAdapter mTypeAdapter;


    @BindView(R.id.iv_search)
    ImageView iv_search;

    @OnClick({R.id.iv_back, R.id.iv_search})
    void onClick(View v){
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_search:
                startActivity(new Intent(mContext, PublishActivity.class)
                .putExtra("position", mViewPager.getCurrentItem()));
                break;
        }
    }
    @Override
    protected int initLayoutId() {
        return R.layout.activity_publish;
    }

    @Override
    protected void initView() {
        mFragments = new ArrayList<>();
        tv_title.setText(getString(R.string.my_publish));
        iv_search.setVisibility(View.VISIBLE);
        iv_search.setImageResource(R.mipmap.top_plus);
        mTabLayout.removeAllTabs();
        datas.add(getString(R.string.buy));
        datas.add(getString(R.string.supply));
        for (int i = 0; i < datas.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(datas.get(i)));
            mTitles.add(datas.get(i));
            mFragments.add(PublishListFragment.newInstance(i+1));
        }
        setPagerAdapter(mFragments, mTitles);
    }

    @Override
    protected void initData() {

    }

    private void setPagerAdapter(List<BaseFragment> mFragments, List<String> mTitles){
        mTypeAdapter = new TypePageAdapter(getSupportFragmentManager());
        mTypeAdapter.setData(mFragments, mTitles);
        mViewPager.setAdapter(mTypeAdapter);
        mViewPager.setOffscreenPageLimit(mTitles.size() - 1);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(mTypeAdapter);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
